package com.hzit.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.dao.ClassesMapper;
import com.hzit.dao.StudentMapper;
import com.hzit.pojo.Classes;
import com.hzit.pojo.PageResult;
import com.hzit.pojo.Student;
import com.hzit.pojo.StudentVo;
import com.hzit.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private ClassesMapper classesMapper;
	//1. 查询所有学生
	@Override
	public List<Student> findAll() {
		List<Student> all = studentMapper.findAll();
		for (Student student : all) {
			Classes classes = classesMapper.findById(student.getCid());
			student.setCname(classes.getCname());
		}
		return all;
	}

	//2. 分页查询
	@Override
	public PageResult<Student> findByPage(int page, int pageSize) {
		//2.1 开始分页
		PageHelper.startPage(page,pageSize);
		//2.2 进行查询
		List<Student> all = findAll();
		//2.3 转换为Page<Student>对象
		Page<Student> studentPage = (Page<Student>) all;
		//2.4 返回
		return new PageResult<>(studentPage.getTotal(),studentPage.getPages(),studentPage.getResult());
	}

	//3. 条件查询学生带分页
	@Override
	public PageResult<Student> search(int page, int pageSize, StudentVo vo) {
		//3.1 开始分页
		PageHelper.startPage(page,pageSize);
		//3.2 开始分页查询
		List<Student> students = studentMapper.search(vo);
		for (Student student : students) {
			Classes classes = classesMapper.findById(student.getCid());
			student.setCname(classes.getCname());
		}
		//3.3 转换为Page<Student>对象
		Page<Student> studentPage = (Page<Student>) students;
		//3.4 返回
		return new PageResult<>(studentPage.getTotal(),studentPage.getPages(),studentPage.getResult());
	}

	//4. 添加学生
	@Override
	public void add(Student student) {
		studentMapper.insert(student);
	}

	//5. 修改学生
	@Override
	public void update(Student student) {
		studentMapper.update(student);
	}

	//6. 根据主键删除学生
	@Override
	public void delete(String sid) {
		studentMapper.delete(sid);
	}
}
